package patterndemo.mediator;

public class MediatorClient {
    public static void main(String[] args) {
        MediatorAbstractColleague collA = new MediatorColleagueA();
        MediatorAbstractColleague collB = new MediatorColleagueB();

        AbstractMediator am = new Mediator(collA, collB);

        System.out.println("==========通过设置A影响B==========");
        collA.setNumber(1000, am);
        System.out.println("collA的number值为：" + collA.getNum());
        System.out.println("collB的number值为A的10倍：" + collB.getNum());

        System.out.println("==========通过设置B影响A==========");
        collB.setNumber(1000, am);
        System.out.println("collB的number值为：" + collB.getNum());
        System.out.println("collA的number值为B的0.1倍：" + collA.getNum());

    }
}

abstract class MediatorAbstractColleague {
    protected int num;

    public abstract void setNumber(int number, AbstractMediator mediator);

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }
}

class MediatorColleagueA extends MediatorAbstractColleague {

    @Override
    public void setNumber(int number, AbstractMediator mediator) {
        this.num = number;
        mediator.AaffectB();
    }
}

class MediatorColleagueB extends MediatorAbstractColleague {

    @Override
    public void setNumber(int number, AbstractMediator mediator) {
        this.num = number;
        mediator.BaffectA();
    }
}


abstract class AbstractMediator {
    protected MediatorAbstractColleague A;
    protected MediatorAbstractColleague B;

    public AbstractMediator(MediatorAbstractColleague a, MediatorAbstractColleague b) {
        A = a;
        B = b;
    }

    public abstract void AaffectB();

    public abstract void BaffectA();
}

class Mediator extends AbstractMediator {

    public Mediator(MediatorAbstractColleague a, MediatorAbstractColleague b) {
        super(a, b);
    }

    @Override
    public void AaffectB() {
        int number = A.getNum();
        B.setNum(number * 100);
    }

    @Override
    public void BaffectA() {
        int number = B.getNum();
        A.setNum(number / 100);
    }
}
